<?php

namespace core\tpl;

use Exception;
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
use ReflectionClass;
use Shopwwi\LaravelCache\Cache;
use support\Db;

class PhpTpl
{


    public static function query(string $sql)
    {
        return Db::query($sql);
    }


    /**
     * 引入文件
     *
     * @param string $template
     * @return void
     */
    public static function introduce(string $template) {}



    /**
     * 安全检查
     * @throws Exception
     * @return void
     */
    private static function  safeCheck()
    {
        //模板标签不允许上传文件
        if (request()->file() != null) {
            throw new Exception('非法参数');
        }
    }


    // public function Parse

    public static function __callStatic($name, $arguments)
    {
        self::safeCheck();
        $cacheId = implode('_', flattenArray(
            request()->get() ?
                array_merge(request()->get(), $arguments)
                : $arguments
        ));
        $cacheData = Cache::get($cacheId);

        if ($cacheData) {
            return $cacheData;
        }
        if (count($arguments) < 2) {
            throw new Exception('模板标签缺少参数' . $name);
        }

        try {
            switch ($name) {
                case 'frame':
                    $reflectionClass = new ReflectionClass('app\\bll\\' . $arguments[0]);
                    break;
                case 'rc':
                    $reflectionClass = new ReflectionClass('app\\bll\\' . $arguments[0]);
                    break;
                default:
                    $reflectionClass = new ReflectionClass('\\plugin\\' . $name . '\\app\\bll\\' . $arguments[0]);
            }

            $method = $reflectionClass->getMethod($arguments[1]);
            foreach ($method->getAttributes() as $attr) {
                if ($attr->getName() == 'core\attribute\Template') {
                    $instance = $reflectionClass->newInstance();
                    unset($arguments[0]);
                    unset($arguments[1]);
                    $data = $method->invoke($instance, ...$arguments);

                    //分页 返回数据和分页条
                    if ($data instanceof LengthAwarePaginator) {
                        $items = $data->items();
                        foreach ($items as $key => $item) {
                            if (is_object($item)) {
                                $items[$key] = (array)$item;
                            }
                        }
                        $data = [
                            'items'       => $items,
                            'paginator'   => paginator($data->total(), $data->perPage()),
                            'total'       => $data->total(),
                            'pagesize'    => $data->perPage(),
                            'currentPage' => $data->currentPage(),
                            'lastPage'    => $data->lastPage(),
                        ];
                    }

                    //JSON格式解析成数组
                    if (is_string($data) && is_json($data)) {
                        $data = json_decode($data, true);
                    }

                    Cache::put($cacheId, $data);
                    return $data;
                    break;
                }
            }
        } catch (Exception $ex) {
            throw new Exception('模板标签不正确' . $name . '|' . implode('|', $arguments) . '|info:' . $ex->getMessage());
        }
        throw new Exception('模板标签不正确1' . $name . '|' . implode('|', $arguments));
    }
}
